package com.xtwv4love.springboot.service;

import com.alibaba.fastjson.JSON;
import com.xtwv4love.springboot.mapper.StudentMapper;
import com.xtwv4love.springboot.pojo.Student;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Component;

import java.util.List;

@Component
public class StudentService {

    @Autowired
    private StudentMapper studentMapper;

    @Autowired
    private RedisTemplate redisTemplate;

    @Autowired
    private StringRedisTemplate stringRedisTemplate;


    public List<Student> finaAllStudents() {
        List<Student> list = redisTemplate.opsForList().range("student:list", 0, -1);
        if (list.size() == 0) {
            list = studentMapper.findAll();
            redisTemplate.opsForList().leftPush("student:list", JSON.toJSONString(list));
        }

        return list;
    }


}
